<?php

require_once 'phpmoko/ui/HTML.php';
require_once 'phpmoko/ui/Widget.php';

class FlexTable extends Widget
{
	private $spacing = 0;
	private $widgets = array();
	private $widgetsIds = array();

	function __construct()
	{
		parent::__construct();
	}

	public function setSpacing($spacing)
	{
		$this->spacing = $spacing;
	}

	/**
	 * @param $x
	 * @param $y
	 * @param Widget $widget
	 * @return unknown_type
	 */
	public function setWidget($x, $y, $widget)
	{
		if ($widget instanceof Widget)
			$this->widgets[] = $widget;
		else
			$this->widgets[] = new HTML($widget);
		
		$this->widgetsIds[$x][$y] = count($this->widgets) - 1;
	}

	/**
	 * @param $x
	 * @param $y
	 * @return Widget
	 */
	public function getWidget($x, $y)
	{
		return $this->widgets[$this->widgetsIds[$x][$y]];
	}

	public function setColSpan($x, $y, $cols)
	{
		for ($i = $x; $i < ($x + $cols); $i++)
			$this->widgetsIds[$i][$y] = $this->widgetsIds[$x][$y];
	}

	public function setCellHorizontalAlignment($x, $y, $alignment)
	{
		$this->widgets[$this->widgetsIds[$x][$y]]->setHorizontalAlignment($alignment);
	}

	public function setCellVerticalAlignment($x, $y, $alignment)
	{
		$this->widgets[$this->widgetsIds[$x][$y]]->setVerticalAlignment($alignment);
	}

	function toString()
	{
		$widthMax = 0;
		$heightMax = 0;
		$width = 0;
		foreach ($this->widgetsIds as $key => $value)
		{
			$height = 0;
			foreach ($this->widgetsIds[$key] as $key2 => $value2)
				$height++;
			
			if ($heightMax < $height)
				$heightMax = $height;
			
			$width++;
		}
		
		if ($widthMax < $width)
			$widthMax = $width;
		
		$width = $widthMax;
		$height = $heightMax;
		
		// Draw the table generated
		$str = '<table cellpadding="0" cellspacing="'.$this->spacing.'"';
		
		if ($this->id !== null)
			$str .= ' id="'.$this->id.'"';
		
		if ($this->getHorizontalAlignment() != null)
			$str .= ' align="'.$this->getHorizontalAlignment().'"';
		
		if ($this->style != null)
			$str .= ' class="'.$this->style.'"';
		
		if ($this->getWidth() != null)
			$str .= ' width="'.$this->getWidth().'"';
		
		$str .= '>';
		
		$draws = array();
		
		for ($i = 0; $i < $height; $i++)
		{
			$str .= '<tr>';
			for ($j = 0; $j < $width; $j++)
			{
				$widgetPos = null;
				
				if (isset($this->widgetsIds[$j][$i]))
					$widgetPos = $this->widgetsIds[$j][$i];
				
				if ((array_key_exists($widgetPos, $draws)) && ($draws[$widgetPos] == -1))
					continue;
				
				$widgetCols = 0;
				$widgetRows = 0;
				for ($k = 0; $k < $width; $k++)
					if (isset($this->widgetsIds[$k][$i]) && ($this->widgetsIds[$k][$i] == $widgetPos))
						$widgetCols++;
				
				for ($k = 0; $k < $height; $k++)
					if (isset($this->widgetsIds[$j][$k]) && ($this->widgetsIds[$j][$k] == $widgetPos))
						$widgetRows++;
				
				$str .= '<td';
				if ($widgetRows > 1)
					$str .= ' rowspan="'.$widgetRows.'"';
				
				if ($widgetCols > 1)
					$str .= ' colspan="'.$widgetCols.'"';
				
				if (($widgetPos !== null) && ($this->widgets[$widgetPos] != null))
				{
					if ($this->widgets[$widgetPos]->getWidth() != null)
						$str .= ' width="'.$this->widgets[$widgetPos]->getWidth().'"';
					
					if ($this->widgets[$widgetPos]->getHorizontalAlignment() != null)
						$str .= ' align="'.$this->widgets[$widgetPos]->getHorizontalAlignment().'"';
					
					if ($this->widgets[$widgetPos]->getVerticalAlignment() != null)
						$str .= ' valign="'.$this->widgets[$widgetPos]->getVerticalAlignment().'"';
				}
				
				$str .= '>';
				
				if (($widgetPos !== null) && ($this->widgets[$widgetPos] != null))
					$str .= $this->widgets[$widgetPos]->toString();
				else
					$str .= '&nbsp;';
				
				$str .= '</td>';
				
				$draws[$widgetPos] = -1;
			}
			
			$str .= '</tr>';
		}
		
		$str .= '</table>';
		
		return $str;
	}
}
?>